package guda.house.biz.form.bill;

import guda.house.biz.util.PriceUtil;
import guda.house.dal.dataobject.InvoiceLogDO;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class InvoiceLogForm {
    @NotNull(message = "不能为空")
    private Long villageId;
    @NotNull(message = "不能为空")
    private Long buildingId;
    @NotNull(message = "不能为空")
    private Long unitId;

    @NotNull(message = "不能为空")
    private Long suiteId;

    @NotNull(message = "不能为空")
    @Pattern(regexp = "^([1-9]\\d*|0)(\\.\\d{1,2})?$", message = "最多2位数的小数")
    private String invoiceMoney;

    @NotNull(message = "不能为空")
    private Long invoiceCatId;

    @NotNull(message = "不能为空")
    private Long invoiceTypeId;
    @Size(min = 0, max = 60, message = "最多60位")
    private String invoiceNo;
    @Size(min = 0, max = 60, message = "最多60位")
    private String invoiceSerial;

    private String gmtInvoice;
    @Size(min = 0, max = 1000, message = "最多1000位")
    private String invoiceComment;
    private String payIdArray;

    public String getInvoiceSerial() {
        return invoiceSerial;
    }

    public void setInvoiceSerial(String invoiceSerial) {
        this.invoiceSerial = invoiceSerial;
    }

    public String getPayIdArray() {
        return payIdArray;
    }

    public void setPayIdArray(String payIdArray) {
        this.payIdArray = payIdArray;
    }

    public InvoiceLogDO toDO() {
        InvoiceLogDO invoiceLogDO = new InvoiceLogDO();
        invoiceLogDO.setInvoiceCatId(invoiceCatId);
        invoiceLogDO.setInvoiceComment(invoiceComment);
        invoiceLogDO.setInvoiceNo(invoiceNo);
        invoiceLogDO.setInvoiceTypeId(invoiceTypeId);
        invoiceLogDO.setSuiteId(suiteId);
        invoiceLogDO.setInvoiceMoney(PriceUtil.convertPriceYuan(invoiceMoney));

        invoiceLogDO.setInvoiceSerial(invoiceSerial);
        if (StringUtils.hasText(gmtInvoice)) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date parse = simpleDateFormat.parse(gmtInvoice);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(parse);
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                invoiceLogDO.setGmtInvoice(calendar.getTime());
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }

        }

        return invoiceLogDO;
    }

    public Long getVillageId() {
        return villageId;
    }

    public void setVillageId(Long villageId) {
        this.villageId = villageId;
    }

    public Long getBuildingId() {
        return buildingId;
    }

    public void setBuildingId(Long buildingId) {
        this.buildingId = buildingId;
    }

    public Long getUnitId() {
        return unitId;
    }

    public void setUnitId(Long unitId) {
        this.unitId = unitId;
    }

    public Long getSuiteId() {
        return suiteId;
    }

    public void setSuiteId(Long suiteId) {
        this.suiteId = suiteId;
    }


    public String getInvoiceMoney() {
        return invoiceMoney;
    }

    public void setInvoiceMoney(String invoiceMoney) {
        this.invoiceMoney = invoiceMoney;
    }

    public Long getInvoiceCatId() {
        return invoiceCatId;
    }

    public void setInvoiceCatId(Long invoiceCatId) {
        this.invoiceCatId = invoiceCatId;
    }

    public Long getInvoiceTypeId() {
        return invoiceTypeId;
    }

    public void setInvoiceTypeId(Long invoiceTypeId) {
        this.invoiceTypeId = invoiceTypeId;
    }

    public String getInvoiceNo() {
        return invoiceNo;
    }

    public void setInvoiceNo(String invoiceNo) {
        this.invoiceNo = invoiceNo;
    }

    public String getGmtInvoice() {
        return gmtInvoice;
    }

    public void setGmtInvoice(String gmtInvoice) {
        this.gmtInvoice = gmtInvoice;
    }

    public String getInvoiceComment() {
        return invoiceComment;
    }

    public void setInvoiceComment(String invoiceComment) {
        this.invoiceComment = invoiceComment;
    }
}
